package com.itheima.mall.web.oms;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.oms.OrderReturnReasonService;
import com.itheima.mall.entity.domain.oms.OrderReturnReason;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 退货原因表 前端控制器
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/returnReason")
public class OrderReturnReasonController {

    @DubboReference
    private OrderReturnReasonService orderReturnReasonService;

    @PostMapping("/create")
    public Result create(@RequestBody OrderReturnReason orderReturnReason){
        orderReturnReasonService.save(orderReturnReason);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(Integer pageNum, Integer pageSize){
        Page<OrderReturnReason> page1 = new Page<>(pageNum,pageSize);
        Page<OrderReturnReason> page = orderReturnReasonService.page(page1);
        return Result.success(PageResult.init(page.getRecords(),page.getCurrent(),page.getSize(),page.getTotal()));
    }

    @PostMapping("/update/status")
    public Result updateStatus(@RequestParam List<Integer> ids,@RequestParam Integer status){
        orderReturnReasonService.update(new LambdaUpdateWrapper<OrderReturnReason>().in(Objects.nonNull(ids),OrderReturnReason::getId,ids).set(Objects.nonNull(status),OrderReturnReason::getStatus,status));
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@PathVariable Integer id,@RequestBody OrderReturnReason orderReturnReason){
        orderReturnReasonService.updateById(orderReturnReason);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        OrderReturnReason byId = orderReturnReasonService.getById(id);
        return Result.success(byId);
    }

}

